package com.tbit.uqbike.service.event.handler;

import com.tbit.uqbike.constant.BrandRemoteConfigConstant;
import com.tbit.uqbike.object.pojo.BrandRemoteConfig;
import com.tbit.uqbike.service.business.BrandRemoteConfigService;
import com.tbit.uqbike.service.business.WeixinPayConfigService;
import com.tbit.uqbike.service.event.EventType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 微信服务商支付 开关变更时间处理器
 *
 * @author <a href="mailto:zhang.zhiliang@tbit.com.cn">Zhang ZhiLiang</a>
 * @version 1.0
 * @see <a href="http://adsl1.tbit.com.cn:21490/pages/viewpage.action?pageId=82182491">订单号规范</a>
 * @since 2024/11/4
 */
@Slf4j
@Component
public class MchPaySwitchEventHandler extends EventHandler{

    @Autowired
    private BrandRemoteConfigService brandRemoteConfigService;
    @Autowired
    private WeixinPayConfigService weixinPayConfigService;

    @Override
    protected boolean match(Object config, EventType eventType) {
        if (Objects.isNull(config) || !(config instanceof BrandRemoteConfig)){
            return false;
        }
        BrandRemoteConfig brandRemoteConfig = (BrandRemoteConfig) config;
        return BrandRemoteConfigConstant.WX_MCH_PAY_SWITCH.equals(brandRemoteConfig.getConfigType());
    }

    @Override
    public void doHandle(Object config, EventType eventType) {
        if (!(config instanceof  BrandRemoteConfig)){
            return;
        }
        BrandRemoteConfig brandRemoteConfig = (BrandRemoteConfig) config;
        log.info("处理微信服务商支付开关变更事件：{},事件类型:{}", config, eventType);
        Integer accountId = brandRemoteConfig.getAccountId();
        if (Objects.isNull(accountId)) {
            return;
        }
        brandRemoteConfigService.removeCache(accountId, BrandRemoteConfigConstant.WX_MCH_PAY_SWITCH);
        weixinPayConfigService.removeCache(accountId,2);
    }
}
